﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XFABManager
{



    public class AssetBundleTools
    {
#if UNITY_EDITOR
        public static bool IsValidAssetBundleFile(string asset_path)
        {
            string ext = Path.GetExtension(asset_path);
            if (ext.Equals( ".dll") || ext.Equals( ".cs") || 
                ext.Equals( ".meta") || ext .Equals(".js") || 
                ext.Equals( ".boo") || ext.Equals( ".jar") || 
                ext.Equals( ".mm") || ext.Equals( ".m") || 
                ext.Equals( ".asmdef") /*|| ext.Equals( ".asset")*/ || 
                ext.Equals("") || ext.Equals(".bat") || ext.EndsWith("shader")) 
            {
                return false;
            }

            return true;
        }


        /// <summary>
        /// 判断一个资源是不是需要单独打包 (暂时认为 .prefab .ttf .fontsettings .asset .TTF 结尾的文件单独打包 或者单个文件大小大于1MB) 
        /// </summary>
        /// <param name="asset_path"></param>
        /// <returns></returns>
        public static bool IsNeedsToBePackagedSeparately(string asset_path) {

            if (new System.IO.FileInfo(asset_path).Length > 1048576 )
                return true;
            
            string ext = Path.GetExtension(asset_path);
            if (ext.Equals(".prefab") || ext.Equals(".ttf") ||
                ext.Equals(".fontsettings") || ext.Equals(".asset") ||
                ext.Equals(".TTF") )
            {
                return true;
            }

            return false;
        }
        public static System.Type GetAssetType(string asset_path) { 
            
            System.Type type = AssetDatabase.GetMainAssetTypeAtPath(asset_path);

            if (type == typeof(Texture2D))
            {
                TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(asset_path); // 获取文件 
                if (importer.textureType == TextureImporterType.Sprite)
                {
                    return typeof(Sprite);
                }
            }
            else if (type == typeof(UnityEditor.SceneAsset))
            {
                return typeof(UnityEngine.SceneManagement.Scene);
            }
            else if (type.FullName == "UnityEditor.Audio.AudioMixerController") { 
                return typeof(UnityEngine.Audio.AudioMixer);
            }

            return type;
        }

        /// <summary>
        /// 根据asset_path 获取到 
        /// </summary>
        /// <param name="asset_path"></param>
        /// <returns></returns>
        public static string GetAssetNameWithType(string asset_path)
        {
            return GetAssetNameWithType(System.IO.Path.GetFileNameWithoutExtension(asset_path), GetAssetType(asset_path));
        }
#endif 
        public static string GetAssetNameWithType(string assetName, System.Type type) {
            return string.Format("{0}_{1}", assetName, type.FullName);
        }

    }




}
